package com.kbplus.demo.data.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kbplus.demo.data.permission.config.CustomException;
import com.kbplus.demo.data.permission.entity.LoginUser;
import com.kbplus.demo.data.permission.entity.Role;
import com.kbplus.demo.data.permission.entity.User;
import com.kbplus.demo.data.permission.entity.UserDept;
import com.kbplus.demo.data.permission.mapper.*;
import com.kbplus.demo.data.permission.service.DepartmentService;
import com.kbplus.demo.data.permission.service.UserService;
import com.kbplus.demo.data.permission.utils.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * (User)表服务实现类
 *
 * @author kbplus
 * @since 2022-05-13 10:32:29
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private TokenService tokenService;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserDeptMapper userDeptMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private DataPermissionMapper dataPermissionMapper;


    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @return 结果
     */
    @Override
    public String login(String username, String password)
    {
        User user1 = baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if(user1==null){
            throw new CustomException("用户不存在");
        }
        if(!password.equals(user1.getPassword())){
            throw new CustomException("密码不正确");
        }
        //这里使用mybaitis特性collection之类的好像会更快，各位有兴趣可以尝试

        Set<Role> allRoleByUserId = roleMapper.getAllRoleByUserId(user1.getId());
        UserDept userDept = userDeptMapper.selectOne(new QueryWrapper<UserDept>().eq("user_id", user1.getId()));
        Set<String> allPositionByUserId = positionMapper.getAllPositionByUserId(user1.getId());
        List<String> deptChildren = departmentService.getDeptChildren(userDept.getDeptId());
        Set<String> allDataPermissionByUserId = dataPermissionMapper.getAllDataPermissionByUserId(user1.getId());

        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user1);
        loginUser.setTenantId(user1.getTenantId());
        loginUser.setRoles(allRoleByUserId);
        loginUser.setUserId(user1.getId());
        loginUser.setDeptId(userDept.getDeptId());
        loginUser.setPostIds(allPositionByUserId);
        loginUser.setDeptChildren(deptChildren);
        loginUser.setDataPermissions(allDataPermissionByUserId);
        String token = UUID.randomUUID().toString();
        loginUser.setToken(token);
        //存入redis
        tokenService.setLoginUser(loginUser);
        // 生成token
        return tokenService.createToken(loginUser);
    }

}

